package com.gg.aliyun.oss.controller;

import com.gg.aliyun.oss.service.OSSApiService;
import com.gg.common.result.CommonResult;
import com.gg.common.result.ResultCodeEnum;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * Author：  chenjunjia
 * Date：    2022/4/21 10:07
 * WeChat：  China_JoJo_
 */
@RestController
@RequestMapping("/api/oss/file")
public class OSSApiController {

    @Resource
    OSSApiService ossApiService;

    @PostMapping("fileUpload")
    public CommonResult fileUpload(@RequestPart("file") MultipartFile file) {
        if (!file.isEmpty()) {
            String url = null;
            try {
                url = ossApiService.fileUpload(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null!=url? new CommonResult(ResultCodeEnum.SUCCESS,url) : new CommonResult(ResultCodeEnum.FAIL,"文件上传失败");
        }
        return new CommonResult(ResultCodeEnum.FAIL,"文件为NULL");
    }
}
